package org.idtem.component.wordnet;

import java.util.ArrayList;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.FSIndex;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.FSArray;
import org.apache.uima.jcas.cas.StringArray;
import org.apache.uima.resource.ResourceConfigurationException;
import org.apache.uima.resource.ResourceInitializationException;
import org.idtem.component.freeling.Freeling2;
import org.idtem.component.wordnet.datatypes.DataTypeSinonimo;
import org.idtem.component.wordnet.datatypes.DataTypeSinonimos;

public class WordNetAnnotator extends JCasAnnotator_ImplBase {

	@SuppressWarnings("unused")
	private String _frase = "";
	
	@Override
	public void process(JCas aJCas) throws AnalysisEngineProcessException {
		FSIndex freeling2Index = aJCas.getAnnotationIndex(Freeling2.type);
		FSIterator freeling2Iter = freeling2Index.iterator();   
		while (freeling2Iter.hasNext()) {
			Freeling2 freelingAnnot = (Freeling2)freeling2Iter.next();
			int begin = freelingAnnot.getBegin();
			int end = freelingAnnot.getEnd();
			String eagles = freelingAnnot.getEagles();
			String lema = freelingAnnot.getLema();
			String prob = freelingAnnot.getProb();
			String word = freelingAnnot.getWord();
			StringArray offsetStringArray = freelingAnnot.getOffsets();				
		  //do something
		
			if (offsetStringArray!=null && offsetStringArray.size()>0){
				
				DBSpanishWordNetAPI spanishWnAPI = DBSpanishWordNetAPI.getInstance();
				try {
					//String offset = spanishWnAPI.ObtenerOffset("n", "auto", 1);
					String pos = "n";
					if (!eagles.isEmpty()){
						pos = (eagles.toLowerCase()).substring(0,1);
					}
					DataTypeSinonimos data = spanishWnAPI.ObtenerSinonimos(lema, pos);
					ArrayList<DataTypeSinonimo> sinonimos = data.getSinonimos();
					
					FSArray arraySyn = new FSArray(aJCas, sinonimos.size());
					StringArray stringArraySys = new StringArray(aJCas, sinonimos.size());
					int i=0;
					for (DataTypeSinonimo dataTypeSinonimo : sinonimos) {
						Synonym syn2 = new Synonym(aJCas);
						syn2.setName(dataTypeSinonimo.getWord());
						syn2.setId(dataTypeSinonimo.getOffset()+"");
						arraySyn.set(i, syn2);
						stringArraySys.set(i++, dataTypeSinonimo.getWord());
					}
		
					// Seteo el array a los sinonimos
					Synonyms syns = new Synonyms(aJCas);
					syns.setBegin(begin);
					syns.setEnd(end);			
					syns.setSynonyms(arraySyn);
					
					//syns.setSinonimos(stringArraySys);
					syns.addToIndexes(aJCas);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
		}
		
		// acceder a las anotaciones de componentes anteriores en el flujo 
		/*FSIndex timeIndex = aJCas.getAnnotationIndex(TimeAnnot.type);
		Iterator timeIter = timeIndex.iterator();   
		while (timeIter.hasNext()) {
		  TimeAnnot time = (TimeAnnot)timeIter.next();

		  //do something
		}*/

	}
	
	@Override
	public void initialize(UimaContext context)
			throws ResourceInitializationException {
		super.initialize(context);
	
		// Get config. parameter values
		this._frase = (String) context.getConfigParameterValue("Frase");
		//String elementoContext = (String) context.getSession().get("martin");
	    

		
		
		
	}
	
	@Override
	public void reconfigure() throws ResourceConfigurationException , ResourceInitializationException
	{
			super.reconfigure();
	//if you take a look at the Javadocs (located in the docs/api directory)
	//for org.apache.uima.analysis_component.AnaysisComponent (which our annotator
	//implements indirectly through JCasAnnotator_ImplBase), you will see that there is a 
	//reconfigure() method, which is called by the containing application through the 
	//UIMA framework, if the configuration parameter values are changed.

	//The AnalysisComponent_ImplBase class provides a default implementation that just calls 
	//the annotator's destroy method followed by its initialize method. This works fine for our 
	//annotator. The only situation in which you might want to override the default reconfigure() 
	//is if your annotator has very expensive initialization logic, and you don't want to 
	//reinitialize everything if just one configuration parameter has changed. In that case, you 
	//can provide a more intelligent implementation of reconfigure() for your annotator.
	
	
	}

}
